package com.cq.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cq.excelModel.ActivityPlatformInfluencergradeGroupExcel;
import com.cq.mapper.ActivityPlatformInfluencergradeGroupMapper;
import com.cq.pojo.ActivityPlatformInfluencergradeGroup;
import com.cq.pojo.Alog;
import com.cq.query.PageResultQuery;
import com.cq.result.PageResult;
import com.cq.service.ActivityPlatformInfluencergradeGroupService;
import com.cq.utils.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityPlatformInfluencergradeGroupServiceImpl implements ActivityPlatformInfluencergradeGroupService {

    @Autowired
    ActivityPlatformInfluencergradeGroupMapper activityPlatformInfluencergradeGroupMapper;

    @Override
    public PageResult<ActivityPlatformInfluencergradeGroup> findActivityPlatformInfluencegradeGroup(PageResultQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());

        List<ActivityPlatformInfluencergradeGroup> list = activityPlatformInfluencergradeGroupMapper.selectActivityPlatformInfluencergradeGroup(query);

        PageInfo<ActivityPlatformInfluencergradeGroup> info = new PageInfo<>(list);

        return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), list);
    }

    @Override
    public void activityPlatformInfluencergradeGroupDownload(HttpServletResponse response, PageResultQuery query) throws IOException {
        PageResult<ActivityPlatformInfluencergradeGroup> pageResult = this.findActivityPlatformInfluencegradeGroup(query);

        List<ActivityPlatformInfluencergradeGroup> list = pageResult.getList();

        String fileName = System.currentTimeMillis() + "";

        List<ActivityPlatformInfluencergradeGroupExcel> collect = list.stream()
                .map(a -> {
                    ActivityPlatformInfluencergradeGroupExcel excel = new ActivityPlatformInfluencergradeGroupExcel();
                    BeanUtils.copyProperties(a, excel);
                    Alog alog = a.getAlog();
                    BeanUtils.copyProperties(alog, excel);
                    return excel;
                }).collect(Collectors.toList());

        EasyExcel.write(response.getOutputStream(), ActivityPlatformInfluencergradeGroupExcel.class)
                .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                .sheet("分活动分媒介分达人等级")
                .doWrite(collect);
    }
}
